﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using HighDiveEngine.Aggregates;

namespace HighDiveEngine.IoEngines
{
    /// <summary>
    /// Handles saving & loading objects to XML-files
    /// </summary>
    public class XmlEngine
    {
        /// <summary>
        /// Saves a Dictionary<string, double> to an .xml file to 
        /// </summary>
        /// <param name="dictionary">Dictionary to save to file</param>
        /// <param name="fileName">Filename to save the dictionary to without extension</param>
        public static void SaveDictionary(Dictionary<string, double> dictionary, string fileName)
        {
            using (TextWriter writer  = new StreamWriter(fileName + ".xml"))
            {
                var serializer = new XmlSerializer(typeof(Item[]), new XmlRootAttribute() { ElementName = "entries" });
                serializer.Serialize(writer, dictionary.Select(kv=>new Item(){Key = kv.Key, Value = kv.Value}).ToArray());
            }
        }

        /// <summary>
        /// Loads a Dictionary<string, double> from a .xml file
        /// </summary>
        /// <param name="fileName">Filename to load the dictionary from without extension</param>
        /// <returns>The loaded dictonary</returns>
        public static Dictionary<string, double> LoadDictionary(string fileName)
        {
            using (TextReader reader = new StreamReader(fileName + ".xml"))
            {
                var serializer = new XmlSerializer(typeof(Item[]), new XmlRootAttribute() { ElementName = "entries" });
                return ((Item[])serializer.Deserialize(reader)).ToDictionary(i => i.Key, i=> i.Value);
            }
        }

        /// <summary>
        /// Helper class used to facilitate serializing a Dictionary<string, double>
        /// </summary>
        public class Item
        {
            public string Key;
            public double Value;
        }

        /// <summary>
        /// Loads a divelist from an xml file
        /// </summary>
        /// <param name="fileName">Filename to load from excluding extension</param>
        public static void LoadDiveList(string fileName)
        {
            using (TextReader reader = new StreamReader(fileName + ".xml"))
            {
                var serializer = new XmlSerializer(typeof(DiveListProxy));
                var diveListProxy = (DiveListProxy)serializer.Deserialize(reader);
                var savedDivers = new List<int?>();
                foreach (var dive in diveListProxy.DiveList)
                {
                    //DbHandler.Instance._saveDiveToDb(dive, diveListProxy.ContestId, savedDivers);
                }
            }
        }

        /// <summary>
        /// Saves a divelist to an xml file
        /// </summary>
        /// <param name="diveList">Divelist to store</param>
        /// <param name="contestId">Database Id of the contest that this divelist is associated with</param>
        /// <param name="fileName">filename to store to excluding extension</param>
        public static void SaveDiveList(DiveList diveList, int contestId, string fileName)
        {
            using (TextWriter writer = new StreamWriter(fileName + ".xml"))
            {
                var serializer = new XmlSerializer(typeof(DiveListProxy));
                serializer.Serialize(writer, new DiveListProxy(){ContestId = contestId, DiveList = diveList});
            }
        }

        /// <summary>
        /// Helper class for storing divelists
        /// </summary>
        public class DiveListProxy
        {
            public int ContestId;
            public DiveList DiveList;
        }

        public static string SerializeObject<T>(T toSerialize)
        {
            var serializer = new XmlSerializer(toSerialize.GetType());
            using (TextWriter textWriter = new StringWriter())
            {
                serializer.Serialize(textWriter, toSerialize);
                return textWriter.ToString();
            }
        }
    }
}
